package ym.controller.front.repair;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ym.pojo.RepairReport;
import ym.service.RepairReportService;
import ym.service.UserService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 维修报备
 */
@RestController
@CrossOrigin
@RequestMapping("/front/repairReport")
public class FrontRepairReportController {
    @Autowired
    private RepairReportService repairReportService;
    @Autowired
    private UserService userService;

    /**
     * 新增
     *
     * @param repairReport
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(RepairReport repairReport) {
        repairReportService.add(repairReport);
        return BaseResult.ok();
    }

    /**
     * 修改
     *
     * @param repairReport
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(RepairReport repairReport) {
        repairReportService.update(repairReport);
        return BaseResult.ok();
    }

    /**
     * 删除
     *
     * @param repairReportId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer repairReportId) {
        repairReportService.delete(repairReportId);
        return BaseResult.ok();
    }

    /**
     * 根据id查询
     *
     * @param repairReportId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer repairReportId) {
        RepairReport repairReport = repairReportService.findById(repairReportId);
        //设置学生和维修员信息
        repairReport.setStudent(userService.findById(repairReport.getStudentId()));
        repairReport.setMaintainer(userService.findById(repairReport.getMaintainerId()));
        return BaseResult.ok(repairReport);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<RepairReport> repairReports = repairReportService.findAll();
        return BaseResult.ok(repairReports);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    public BaseResult search(String search, int page, int size) {
        Page<RepairReport> repairReportPage = repairReportService.search(search, page, size);
        return BaseResult.ok(repairReportPage);
    }

    /**
     * 根据用户id分页查询,状态为待处理/已接单
     *
     * @param userId
     * @param search
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/searchByUserIdByStatus1")
    public BaseResult searchByUserIdByStatus1(Integer userId, String search, int page, int size) {
        Page<RepairReport> repairReportPage = repairReportService.searchByUserId(userId, search, page, size);
        //遍历列表，删除repairReportStatus为已完成的数据
        List<RepairReport> repairReports = repairReportPage.getRecords();
        for (int i = 0; i < repairReports.size(); i++) {
            if (repairReports.get(i).getRepairReportStatus().equals("已完成")) {
                repairReports.remove(i);
                i--;
            }
        }
        //历列表，设置学生和维修员信息
        for (int i = 0; i < repairReports.size(); i++) {
            repairReports.get(i).setStudent(userService.findById(repairReports.get(i).getStudentId()));
            repairReports.get(i).setMaintainer(userService.findById(repairReports.get(i).getMaintainerId()));
        }
        repairReportPage.setRecords(repairReports);
        return BaseResult.ok(repairReportPage);
    }

    /**
     * 根据用户id分页查询,状态为已完成
     *
     * @param userId
     * @param search
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/searchByUserIdByStatus2")
    public BaseResult searchByUserIdByStatus2(Integer userId, String search, int page, int size) {
        Page<RepairReport> repairReportPage = repairReportService.searchByUserId(userId, search, page, size);
        //遍历列表，只要repairReportStatus为已完成的数据
        List<RepairReport> repairReports = repairReportPage.getRecords();
        for (int i = 0; i < repairReports.size(); i++) {
            if (!repairReports.get(i).getRepairReportStatus().equals("已完成")) {
                repairReports.remove(i);
                i--;
            }
        }
        //历列表，设置学生和维修员信息
        for (int i = 0; i < repairReports.size(); i++) {
            repairReports.get(i).setStudent(userService.findById(repairReports.get(i).getStudentId()));
            repairReports.get(i).setMaintainer(userService.findById(repairReports.get(i).getMaintainerId()));
        }
        repairReportPage.setRecords(repairReports);
        return BaseResult.ok(repairReportPage);
    }

    /**
     * 分页查询,状态为待处理
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/searchByStatus")
    public BaseResult searchByStatus(String search, int page, int size) {
        Page<RepairReport> repairReportPage = repairReportService.search(search, page, size);
        //遍历列表，只要repairReportStatus为待处理的数据
        List<RepairReport> repairReports = repairReportPage.getRecords();
        for (int i = 0; i < repairReports.size(); i++) {
            if (!repairReports.get(i).getRepairReportStatus().equals("待处理")) {
                repairReports.remove(i);
                i--;
            }
        }
        //历列表，设置学生信息
        for (int i = 0; i < repairReports.size(); i++) {
            repairReports.get(i).setStudent(userService.findById(repairReports.get(i).getStudentId()));
//            repairReports.get(i).setMaintainer(userService.findById(repairReports.get(i).getMaintainerId()));
        }
        repairReportPage.setRecords(repairReports);
        return BaseResult.ok(repairReportPage);
    }


    /**
     * 接单
     *
     * @param repairReportId
     * @param maintainerId
     * @param repairReportExpectedArrival
     * @return
     */
    @PostMapping("/take")
    public BaseResult take(Integer repairReportId, Integer maintainerId, String repairReportExpectedArrival) {
        RepairReport repairReport = repairReportService.findById(repairReportId);
        repairReport.setRepairReportStatus("已接单");//设置接单状态
        repairReport.setMaintainerId(maintainerId);//设置维修员id
        repairReport.setRepairReportExpectedArrival(repairReportExpectedArrival);//设置预期到达时间
        //获取当前时间并设置为接单时间
        String repairReportAcceptTime = java.time.LocalDateTime.now().toString();
        repairReport.setRepairReportAcceptTime(repairReportAcceptTime);
        repairReportService.update(repairReport);
        return BaseResult.ok();
    }

    /**
     * 根据维修员id分页查询,状态为已接单
     *
     * @param maintainerId
     * @param search
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/searchByMaintainerIdByStatus1")
    public BaseResult searchByMaintainerIByStatus1(Integer maintainerId, String search, int page, int size) {
        Page<RepairReport> repairReportPage = repairReportService.searchByMaintainerId(maintainerId, search, page, size);
        //遍历列表，只要repairReportStatus为已接单的数据
        List<RepairReport> repairReports = repairReportPage.getRecords();
        for (int i = 0; i < repairReports.size(); i++) {
            if (!repairReports.get(i).getRepairReportStatus().equals("已接单")) {
                repairReports.remove(i);
                i--;
            }
        }
        //历列表，设置学生和维修员信息
        for (int i = 0; i < repairReports.size(); i++) {
            repairReports.get(i).setStudent(userService.findById(repairReports.get(i).getStudentId()));
            repairReports.get(i).setMaintainer(userService.findById(repairReports.get(i).getMaintainerId()));
        }
        repairReportPage.setRecords(repairReports);
        return BaseResult.ok(repairReportPage);
    }

}
